package com.shopping.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.auto.JSONAuthentication;
import com.shopping.entity.ShNotice;
import com.shopping.mapper.ShNoticeMapper;
import com.shopping.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shopping
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/sh-notice")
public class ShNoticeController extends JSONAuthentication {
    @Autowired
    ShNoticeMapper shNoticeMapper;
    @ApiOperation(value = "添加通知")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("notice") String notice,@RequestParam("userId") int userId) throws  Exception{
        ShNotice shNotice=new ShNotice();
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        shNotice.setNotice(notice);
        shNotice.setUserId(userId);
        shNotice.setCreatTime(timestamp);
        if(shNoticeMapper.insert(shNotice)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除通知")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=shNoticeMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "通过用户id查询通知")
    @PostMapping("/findByUserId")
    public void findByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId") int   userId,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<ShNotice> shUserPage=new Page<>(current,size);
        Page<ShNotice> shUserPages=shNoticeMapper.selectPage(shUserPage,wrapper);
        List<ShNotice> records = shUserPages.getRecords();
        if(records.size()>0){
            Result ok = Result.ok().data("notice",records);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

